<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <el-menu
        :default-active="activeMenu"
        class="side-menu"
        :router="true"
      >
        <template v-if="authStore.userRole === 'student'">
          <el-menu-item index="/student">
            <el-icon><HomeFilled /></el-icon>
            <span>主页</span>
          </el-menu-item>
          <el-menu-item index="/student/exams">
            <el-icon><Calendar /></el-icon>
            <span>考试列表</span>
          </el-menu-item>
          <el-menu-item index="/student/results">
            <el-icon><Document /></el-icon>
            <span>成绩查询</span>
          </el-menu-item>
          <el-menu-item index="/student/grade-appeal">
            <el-icon><Warning /></el-icon>
            <span>成绩申诉</span>
          </el-menu-item>
        </template>

        <template v-if="authStore.userRole === 'teacher'">
          <el-menu-item index="/teacher">
            <el-icon><HomeFilled /></el-icon>
            <span>主页</span>
          </el-menu-item>
          <el-sub-menu index="question">
            <template #title>
              <el-icon><Edit /></el-icon>
              <span>试题管理</span>
            </template>
            <el-menu-item index="/teacher/questions">试题列表</el-menu-item>
            <el-menu-item index="/teacher/questions/category">分类管理</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="exam">
            <template #title>
              <el-icon><Calendar /></el-icon>
              <span>考试管理</span>
            </template>
            <el-menu-item index="/teacher/exams">考试列表</el-menu-item>
            <el-menu-item index="/teacher/templates">考试模板</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="grading">
            <template #title>
              <el-icon><Check /></el-icon>
              <span>评分管理</span>
            </template>
            <el-menu-item index="/teacher/auto-grading">自动评分</el-menu-item>
            <el-menu-item index="/teacher/manual-grading">人工评分</el-menu-item>
            <el-menu-item index="/teacher/grade-analysis">成绩分析</el-menu-item>
          </el-sub-menu>
        </template>

        <template v-if="authStore.userRole === 'admin'">
          <el-menu-item index="/admin">
            <el-icon><HomeFilled /></el-icon>
            <span>主页</span>
          </el-menu-item>
          <el-menu-item index="/admin/users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/notifications">
            <el-icon><Bell /></el-icon>
            <span>通知管理</span>
          </el-menu-item>
          <el-sub-menu index="system">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/admin/system">系统设置</el-menu-item>
            <el-menu-item index="/admin/backup">数据备份</el-menu-item>
            <el-menu-item index="/admin/logs">系统日志</el-menu-item>
          </el-sub-menu>
        </template>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header>
        <div class="header-content">
          <h2>C程序设计考试系统</h2>
          <el-dropdown @command="handleCommand">
            <span class="user-dropdown">
              {{ authStore.user?.name }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import {
  HomeFilled,
  Calendar,
  Document,
  Warning,
  Edit,
  ArrowDown,
  User,
  Bell,
  Setting,
  Check
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

const activeMenu = computed(() => route.path)

const handleCommand = (command) => {
  if (command === 'logout') {
    authStore.logout()
    router.push('/login')
  } else if (command === 'profile') {
    router.push(`/${authStore.userRole}/profile`)
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.side-menu {
  height: 100%;
  border-right: none;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.user-dropdown {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
}

.el-aside {
  background-color: #304156;
  color: #fff;
}
</style> 